package view

import (
	"AppBigDevops/src/web/middleware"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// 配置路由
func ConfigRouters(r *gin.Engine) {

	// 定义一个组
	// 共享中间件
	base := r.Group("/basic-api")
	{
		base.GET("/ping", Ping)
		base.GET("/now", GetNowTs)
		base.GET("/long", LongRequest)
		base.POST("/login", UserLogin)
	}

	// 下面的group都要求有认证了
	afterLoginApiGroup := r.Group("/api")
	afterLoginApiGroup.Use(middleware.JWTAuthMiddleware())
	{
		afterLoginApiGroup.GET("/userinfo", getUserInfoAfterLogin)
	}

}

func Ping(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "pong",
	})
}

func GetNowTs(c *gin.Context) {
	c.String(200, time.Now().Format("2006-01-02 15:04:05"))
}

func LongRequest(c *gin.Context) {
	fmt.Printf("longRequest请求开始, 休息6秒\n URL: %v \n", c.Request.URL)
	time.Sleep(6 * time.Second)
	c.String(200, "ok")
}
